username and password at https://twitter.com/.username and passwordCreate a new AppCreate a Twitter app at https://apps.twitter.com/.
Fill in with you App name, brief description and a website. Ideally this website is where people can find more information about your app. However, if you don’t have one just insert a valid link to a web page.
As callback URL set: http://127.0.0.1:1410
Setup your app information andcallback URL.
Create your Twitter Application you will be re-directed to your application page. You’re almost there! Go to the Keys and Access Tokens tab and at the bootom of this page click on Create my access token. Your access token will appear at the bottom of the page.Create access token to connect R with Twitter.
R with Twitter! From your application page you need four things:Create access token to connect R with Twitter.
There are two well-known packages used to collect data from Twitter directly into R:
twitteR packageThe function twitteR::searchTwitter will return a list of tweets which you can easily coherce to standard data.frame with the function twitteR::twListToDF.
> api_key <- "your_api_key"
> api_secret <- "your_api_secret"
> token <- "your_token"
> token_secret <- "your_token_secret"
>
> setup_twitter_oauth(api_key, api_secret, token, token_secret)
>
> # Search tweets
> MyTweets <- searchTwitter("#MyChosenHastags", lang = "en")
rtweet packageNote: Remember to set set_renv=FALSE when you run the rtweet::create_token() function. The default should be set to FALSE as mentioned in the help page ?rtweet but it is instead set to TRUE. If set_renv=TRUE it will save a hidden token named .rtweet_token.rds which will be used as the default evironemnt twietter token variable.
More information about this can be found at Obtaining and using access tokens.
The function rtweet::search_tweets returns tweets already as a data frame.
> appname <- "Rladies_app"
> api_key <- "your_api_key"
> api_secret <- "your_api_secret"
>
> setup_twitter_oauth(api_key, api_secret, token, token_secret)
>
> ## create token named 'twitter_token'
> twitter_token <- create_token(app = appname, consumer_key = key, consumer_secret = secret)
>
> MyTweet <- search_tweets("#MyChosenHastags", lang = "en", token = twitter_token)